
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include "rp2040.h"
#include "hardware/structs/uart.h"
#include "hardware/clocks.h"
#include "hardware/resets.h"
#include "hardware/gpio.h"
#include "hardware/i2c.h"

/**/
#include "pico.h"
#include "pico/multicore.h"

#include "umsg.h"
#include "ttusb_comn.h"
#include "ttusb.h"



void tttdelay( uint32_t cnt )
{
	uint32_t  i;

	for (i=0; i<cnt; i++ ) {
		__NOP();
	}
}



static uint32_t ttt_ticks = 0;

void  SysTick_Handler( void )
{
	ttt_ticks += 1;

#if 0
	if ( ttt_ticks & 0x2 )  {
		gpio_put( 25, true );
	} else {
		gpio_put( 25, false );
	}
#endif

	return;
}


void HardFault_exception( void )
{
	printf( "hard fault ...\n" );
	while ( 1 )  {
		__asm volatile ( "nop" );
		__asm volatile ( "wfi" );
	}
}


extern void core_zero_proc_msg( void );
extern void core1_main( void );
extern int utchan_init();

int main( void )
{
	int iret;
	uint32_t  temp;

	/**/
	iret = umsg_init(64);
	printf( "umsg pool, ret = %d\n", iret );

	/* usb auto */
	ttusb_desc_init();
	ttusb_desc_device_vid_pid( 0x2E8A, 0x1013 );
	ttusb_desc_device_str_manu( "TTusb" );
	ttusb_desc_device_str_prod( "Dapk" );
	ttusb_desc_config_power( 0x64 );
	ttusb_desc_interface_add_inp( umsg_inp_cbk );
	ttusb_desc_interface_add_outp( umsg_outp_cbk );
	ttusb_init();

	/* core-1 */
	multicore_launch_core1_raw( core1_main, (uint32_t *)(uintptr_t)0x20042000, 0x10000100 );
	NVIC_EnableIRQ( SIO_IRQ_PROC0_IRQn );

	utchan_init();
	
	/**/
	SysTick_Config( 9000000 );
	printf("hello...\n");

	/**/
	while ( 1 )  {

		/**/
		core_zero_proc_msg();

		/**/
		__WFI();
	}

	return 0;
}

